package arkham.data;

import arkham.jaxb.encounter.*;
import arkham.util.AHLogger;
import arkham.util.XmlTools;

public class Rumor {
	
	private String name;
	private String location;
	private int currentTokens;
	private String startTokenString;
	private String special;
	private String description;
	private EventNode ongoingEffect;
	private EventNode passEffect;
	private EventNode failEffect;
	private EffectType passCondition;
	private EffectType failCondition;
	
	public Rumor(String name)
	{
		this.name = name;
		special = "";
		startTokenString = "";
		description = "";
		currentTokens = 0;
	}
	
	/**
	 * Initializes the rumor based on number of investigators
	 * @param numPlayers number of investigators
	 */
	public void init(int numPlayers)
	{
		if (startTokenString.startsWith("P*"))
		{
			String sval = startTokenString.substring(2);
			currentTokens = Integer.parseInt(sval) * numPlayers;
		}
		else
		{
			currentTokens = Integer.parseInt(startTokenString);
		}
	}

	/**
	 * Get the name of the rumor
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * Gets the location in Arkham where activity takes place
	 * @return the location
	 */
	public String getLocation() {
		return location;
	}

	/**
	 * Gets the number of starting tokens, -1 if it does not use tokens
	 * @return the startTokens
	 */
	public int getCurrentTokens() {
		return currentTokens;
	}
	
	/**
	 * Modifies the number of tokens by a specified amount
	 * @param amount the amount to modify by
	 */
	public void modifyCurrentTokens(int amount)
	{
		currentTokens += amount;
	}
	
	/**
	 * Gets the string representing the number of start tokens
	 * @return the start token string
	 */
	public String getStartTokenString()
	{
		return startTokenString;
	}

	/**
	 * Gets the special property of this rumor, if any
	 * @return the special property of this rumor
	 */
	public String getSpecial() {
		return special;
	}
	
	/**
	 * Gets the description of this rumor
	 * @return the description
	 */
	public String getDescription()
	{
		return description;
	}

	/**
	 * Gets the event node describing the ongoing effect at mythos phase
	 * @return the ongoing effect
	 */
	public EventNode getOngoingEffect() {
		return ongoingEffect;
	}

	/**
	 * Gets the event node describing the effect if the rumor is passed
	 * @return the pass effect
	 */
	public EventNode getPassEffect() {
		return passEffect;
	}

	/**
	 * Gets the event node describing the effect if the rumor is failed
	 * @return the fail effect
	 */
	public EventNode getFailEffect() {
		return failEffect;
	}

	/**
	 * Gets the passing condition
	 * @return the passCondition
	 */
	public EffectType getPassCondition() {
		return passCondition;
	}

	/**
	 * Gets the failing condition
	 * @return the failCondition
	 */
	public EffectType getFailCondition() {
		return failCondition;
	}
	
	@Override
	public String toString()
	{
		return name;
	}
	
	/**
	 * Creates the data representation of a rumor from the XML info
	 * and performs a stringent validation check to make sure the XML data is
	 * valid. 
	 * <br>Will return <b>null</b> on a failure.
	 * 
	 * @param xmlrumor the XML rumor info
	 * @return the data representation of the rumor card
	 */
	public static Rumor createRumorFromXml(RumorType xmlrumor)
	{
		Rumor rumor = new Rumor(xmlrumor.getName());
		rumor.location = xmlrumor.getLocation();
		rumor.startTokenString = xmlrumor.getStartTokens();
		if (xmlrumor.getDescription() != null)
		{
			rumor.description = xmlrumor.getDescription();	
		}
		if (rumor.startTokenString.startsWith("P*"))
		{
			if (!XmlTools.isNumeric(rumor.startTokenString.substring(2)))
			{
				AHLogger.error("Rumor " + rumor + " has invalid start token string: " +
						rumor.startTokenString);
				return null;
			}
		}
		else
		{
			if (!XmlTools.isNumeric(rumor.startTokenString))
			{
				AHLogger.error("Rumor " + rumor + " has invalid start token string: " +
						rumor.startTokenString);
				return null;
			}
		}
		
		if (xmlrumor.getSpecial() != null)
		{
			rumor.special = xmlrumor.getSpecial();
		}
		rumor.ongoingEffect = xmlrumor.getOngoingEffect();
		rumor.passCondition = xmlrumor.getPassCondition();
		rumor.failCondition = xmlrumor.getFailCondition();
		rumor.passEffect = xmlrumor.getPassEffect();
		rumor.failEffect = xmlrumor.getFailEffect();
		
		return rumor;
	}
}

